package com.army.lightweightdb.slice;

import com.army.lightweightdb.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;

public class MainAbilitySlice extends AbilitySlice {
    Context context;
    private Button btnWrite;
    private Button btnRead;
    private Button btnDelete;
    private TextField fruitField;
    private TextField numberField;
    private String fileName;
    private Preferences preferences;
    private DatabaseHelper databaseHelper;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        context = getContext();
        btnWrite = (Button) findComponentById(ResourceTable.Id_write_btn);
        btnRead = (Button) findComponentById(ResourceTable.Id_read_btn);
        btnDelete = (Button) findComponentById(ResourceTable.Id_delete_btn);
        fruitField = (TextField) findComponentById(ResourceTable.Id_text_fruit);
        numberField = (TextField) findComponentById(ResourceTable.Id_text_number);

        databaseHelper = new DatabaseHelper(context);
        fileName = "pdb";
        preferences = databaseHelper.getPreferences(fileName);

        // 初始化操作方法
        btnWrite();
        btnRead();
        btnDelete();
    }

    /**
     * 写入数据库内容
     */
    private void btnWrite() {
        btnWrite.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                String fruit = fruitField.getText();
                try {
                    int number = Integer.parseInt(numberField.getText());
                    preferences.putString("fruit", fruit);
                    preferences.putInt("number", number);
                    preferences.flush();
                    new ToastDialog(context).setText("数据写入成功").show();
                }catch (Exception e) {
                    new ToastDialog(context).setText("请输入数字类型的数字").show();
                }
            }
        });
    }

    /**
     * 读取数据库内容
     */
    private void btnRead() {
        btnRead.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                String str = String.format("水果: %s, 数量: %d",
                        preferences.getString("fruit", ""),
                        preferences.getInt("number", 0));
                new ToastDialog(context).setText(str).show();
            }
        });
    }

    /**
     * 删除数据库文件
     */
    private void btnDelete() {
        btnDelete.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (databaseHelper.deletePreferences(fileName)) {
                    preferences.clear();
                    new ToastDialog(context).setText("删除数据库文件成功").show();
                }else {
                    new ToastDialog(context).setText("删除数据库文件失败").show();
                }
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
